package cn.bigcypress.common.config;


import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
//import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/*Swagger2配置*/
@EnableSwagger2WebMvc
@Configuration
public class SwaggerConfig {

        @Bean(value = "defaultApi2")
        public Docket createRestApi() {
            ParameterBuilder tokenPar = new ParameterBuilder();
            List<springfox.documentation.service.Parameter> pars = new ArrayList<>();
            tokenPar.name("token").description("令牌")
                    .modelRef(new ModelRef("string")).parameterType("query").required(false).build();
            pars.add(tokenPar.build());
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    //配置文件扫描路径
                    .apis(RequestHandlerSelectors.basePackage("cn.bigcypress.controller"))
                    .paths(PathSelectors.any())
                    .build().globalOperationParameters(globalParameters());
        }

    private List<Parameter> globalParameters() {
        ParameterBuilder builder = new ParameterBuilder()
                .description("用户凭证")
                .name("Authentication")
                .modelRef(new ModelRef("string"))
                .parameterType("header");
        return Lists.newArrayList(builder.build());
    }

    @SuppressWarnings("deprecation")
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("故事里的青春SpringBoot项目 后台服务API接口文档")
                .description("使用 knife4j 搭建的后台服务API接口文档")
                .termsOfServiceUrl("http://localhost:8088/")
                .contact("金鑫、秦少杰")
                .version("1.0")
                .build();
    }

    }
